{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#1.使用random,rand方法创建一组服从0-1均匀分布的随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = random.randint(1,10) #生成一个1-10之间的随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "r1 = np.random.rand(n) #生成一组服从0-1分布的随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.36567455 0.70482601 0.55212815 0.68168778 0.57976823 0.77783076\n",
      " 0.33053279 0.79263426 0.15907278]\n"
     ]
    }
   ],
   "source": [
    "print(r1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#2.使用random.uniform方法创建一个均匀分布[low,height）的随机采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "r2 = np.random.uniform(1,10,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#low: 采样下界，float类型，默认值为0\n",
    "#high: 采样上界，float类型，默认值为1\n",
    "#size: 输出样本数目，为int或元组(tuple)类型，例如，size=(m,n,k), 则输出m*n*k个样本，缺省时输出1个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5.14746472]\n"
     ]
    }
   ],
   "source": [
    "print(r2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#3.使用随机数发生器创建取值分别为1，2，3，4，相应概率为0.3、0.2、0.1\n",
    "#、0.4概率分布如下，15行8列的离散分布随机数表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "#random.choice(a,size=None,replace,p=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "r3 = np.random.choice([1,2,3,4],(15,8),replace=True,p=[0.3,0.2,0.1,0.4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 4 1 4 1 1 2 3]\n",
      " [2 4 1 2 2 4 2 2]\n",
      " [4 1 4 1 4 4 1 2]\n",
      " [2 4 4 4 4 3 1 4]\n",
      " [2 1 1 1 3 3 4 4]\n",
      " [1 2 2 3 2 4 1 2]\n",
      " [4 4 3 1 2 4 1 1]\n",
      " [2 4 1 4 2 2 2 4]\n",
      " [2 2 4 4 3 1 4 1]\n",
      " [2 4 2 4 4 2 2 1]\n",
      " [1 1 4 4 4 4 1 4]\n",
      " [4 4 1 4 1 4 4 4]\n",
      " [4 3 4 4 1 2 4 4]\n",
      " [4 1 4 2 1 4 4 4]\n",
      " [2 4 4 4 3 2 1 3]]\n"
     ]
    }
   ],
   "source": [
    "print(r3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "#4.生成一组服从正态分布的随机数，并绘制正态分布直方图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "r4 = np.random.randn(1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif']=['SimHei']#用来正常显示中文标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['axes.unicode_minus']=False#用来显示负号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 720x432 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig=plt.figure(figsize=(10,6))#绘图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([  7.,   7.,  11.,   9.,  27.,  27.,  34.,  49.,  64.,  74.,  92.,\n",
       "         92., 103.,  83.,  72.,  69.,  50.,  38.,  28.,  22.,  15.,  11.,\n",
       "          7.,   5.,   4.]),\n",
       " array([-2.65752082, -2.43516548, -2.21281014, -1.9904548 , -1.76809945,\n",
       "        -1.54574411, -1.32338877, -1.10103343, -0.87867809, -0.65632275,\n",
       "        -0.4339674 , -0.21161206,  0.01074328,  0.23309862,  0.45545396,\n",
       "         0.6778093 ,  0.90016465,  1.12251999,  1.34487533,  1.56723067,\n",
       "         1.78958601,  2.01194135,  2.2342967 ,  2.45665204,  2.67900738,\n",
       "         2.90136272]),\n",
       " <BarContainer object of 25 artists>)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXMAAAD2CAYAAAAksGdNAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAALgklEQVR4nO3dX4he+V3H8ffH2IWwkTZLhkhAGwOJF/6JlqEmEktanGIurLBit1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w584fA3sn2vpH7liRtYMzAvcnq1ArAceDWiH1LkjYw5jTLa8C1JIeAM8CJEfveydpNHT3Gc9udup5b1/OCEc4tVTVGIaudJfuBJeBrVXVntI4lSRsaNcwlSfPhRUpJmqMkzyVZSnJgln4M8xkleWeSrya5kuRLSZ6Zd01jSXIwybV51zG2ziuVO/7Omn/G9gN/B7wX+KckC0P7Msxn91Hglar6IHAH+KU51zOKyR/ZF1hdP9BG55XKXX9nNP2MTfw08Imq+iPgH3hifc5mGOYzqqpXq+rqZHcB+K951jOi7wIvAA/mXcjITvPWlcpdtPydNf6MUVX/XFU3kryP1dH59aF9+bVxm5Tk88CPP9b0j1X1mSQngf1VdWNOpc1kg/OaV0lbZcOVyrtZVT0AaPg7A2C3f8bWk9Vf2AvAPeD/hvZjmG9SVf3Wk21JngM+C/zq9lc0jrXOqylXKu9CHT5j66nVWwpfSvKHwIeAvxnSj3/IM5pcjPki8KmqenPe9eipXKm8y3T+jCX5vSQfm+y+C/jO0L4M89l9nNX/qn86yetJXph3QdrQa8CLSV4BPgx8Zb7laAqdP2MXWf17/Bqwh9XrOIO4aEhvO65UVkeGuSQ14DSLJDVgmEtSA4a5JDVgmEtSA4a5JDXw/6FbaMfE3ZcJAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.hist(r4,25,alpha=0.5)#hist生成直方图"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
